import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
import _createClass from "@babel/runtime/helpers/esm/createClass";
import _inherits from "@babel/runtime/helpers/esm/inherits";
import _createSuper from "@babel/runtime/helpers/esm/createSuper";
import _applyDecoratedDescriptor from "@babel/runtime/helpers/esm/applyDecoratedDescriptor";

var _dec, _dec2, _dec3, _dec4, _dec5, _dec6, _dec7, _dec8, _dec9, _dec10, _dec11, _dec12, _dec13, _dec14, _class, _class2;

/**
 * MessageQuery - 消息查询列表
 * @date: 2018-7-29

 */
import React, { PureComponent } from 'react';
import { connect } from 'dva';
import { Bind } from 'lodash-decorators';
import { Content, Header } from 'components/Page';
import { Button as ButtonPermission } from 'components/Permission';
import { DEFAULT_DATETIME_FORMAT } from 'utils/constants';
import intl from 'utils/intl';
import formatterCollections from 'utils/intl/formatterCollections';
import notification from 'utils/notification';
import { isTenantRoleLevel } from 'utils/utils';
import ContentView from "./ContentView";
import RecipientView from "./RecipientView";
import ListTable from "./ListTable";
import QueryForm from "./QueryForm";
/**
 * 消息查询数据展示列表
 * @extends {PureComponent} - React.PureComponent
 * @reactProps {Object} formValues - 查询表单值
 * @reactProps {Object} recordData - 表格中信息的一条记录
 * @return React.element
 */

function getRefFieldsValue(ref) {
  if (ref.current) {
    return ref.current.props.form.getFieldsValue();
  }

  return {};
}

var MessageQuery = (_dec = connect(function (_ref) {
  var messageQuery = _ref.messageQuery,
      loading = _ref.loading;
  return {
    messageQuery: messageQuery,
    tenantRoleLevel: isTenantRoleLevel(),
    queryMessageLoading: loading.effects['messageQuery/queryMessageList'],
    queryRecipientLoading: loading.effects['messageQuery/queryRecipient'],
    queryContentLoading: loading.effects['messageQuery/queryContent'],
    queryErrorLoading: loading.effects['messageQuery/queryError'],
    deleteLoading: loading.effects['messageQuery/deleteMessage'],
    resendLoading: loading.effects['messageQuery/resendMessage']
  };
}), _dec2 = formatterCollections({
  code: ['cmsg.messageQuery', 'entity.tenant', 'cmsg.common']
}), _dec3 = Bind(), _dec4 = Bind(), _dec5 = Bind(), _dec6 = Bind(), _dec7 = Bind(), _dec8 = Bind(), _dec9 = Bind(), _dec10 = Bind(), _dec11 = Bind(), _dec12 = Bind(), _dec13 = Bind(), _dec14 = Bind(), _dec(_class = _dec2(_class = (_class2 = /*#__PURE__*/function (_PureComponent) {
  _inherits(MessageQuery, _PureComponent);

  var _super = _createSuper(MessageQuery);

  function MessageQuery(props) {
    var _this;

    _classCallCheck(this, MessageQuery);

    _this = _super.call(this, props);
    _this.state = {
      formValues: {},
      contentVisible: false,
      // 内容和错误模态框是否可见
      recipientVisible: false,
      // 收件人模态框是否可见
      isContent: true,
      // 是否为内容
      recordData: {},
      selectedRows: [] // 选择的行数据

    };
    _this.multiSearchFormRef = React.createRef();
    return _this;
  }
  /**
   * 初始化数据
   *
   * @memberof MessageQuery
   */


  _createClass(MessageQuery, [{
    key: "componentDidMount",
    value: function componentDidMount() {
      this.handleQueryMessage();
      this.props.dispatch({
        type: 'messageQuery/init'
      });
    }
    /**
     * 获取消息列表
     *
     * @param {*} [params={}]
     * @memberof MessageQuery
     */

  }, {
    key: "handleQueryMessage",
    value: function handleQueryMessage() {
      var params = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
      var dispatch = this.props.dispatch;
      dispatch({
        type: 'messageQuery/queryMessageList',
        payload: params
      });
    }
    /**
     * 点击内容查看模态框
     *
     * @param {*} record
     * @memberof MessageQuery
     */

  }, {
    key: "handleOpenContentModal",
    value: function handleOpenContentModal(record) {
      var dispatch = this.props.dispatch;
      dispatch({
        type: 'messageQuery/queryContent',
        payload: record.messageId
      });
      this.setState({
        isContent: true,
        contentVisible: true
      });
    }
    /**
     * 确认内容和错误模态框
     *
     * @memberof MessageQuery
     */

  }, {
    key: "handleOk",
    value: function handleOk() {
      var isContent = this.state.isContent;
      this.setState({
        contentVisible: false,
        isContent: !isContent
      });
    }
    /**
     * 收件人查看数据
     *
     * @param {*} record
     * @memberof MessageQuery
     */

  }, {
    key: "handleOpenRecipientModal",
    value: function handleOpenRecipientModal(record) {
      var dispatch = this.props.dispatch;
      dispatch({
        type: 'messageQuery/queryRecipient',
        payload: record
      });
      this.setState({
        recipientVisible: true,
        recordData: record
      });
    }
    /**
     * 确认收件人模态框
     *
     * @memberof MessageQuery
     */

  }, {
    key: "handleRecipientOk",
    value: function handleRecipientOk() {
      this.setState({
        recipientVisible: false
      });
    }
    /**
     * 点击错误查看模态框
     *
     * @param {*} record
     * @memberof MessageQuery
     */

  }, {
    key: "handleOpenErrorModal",
    value: function handleOpenErrorModal(record) {
      var dispatch = this.props.dispatch;
      dispatch({
        type: 'messageQuery/queryError',
        payload: record.transactionId
      });
      this.setState({
        isContent: false,
        contentVisible: true
      });
    }
    /**
     * 重试
     *
     * @param {*} record
     * @memberof MessageQuery
     */

  }, {
    key: "handleResendMessage",
    value: function handleResendMessage(record) {
      var dispatch = this.props.dispatch;
      dispatch({
        type: 'messageQuery/resendMessage',
        payload: record.transactionId
      }).then(function (res) {
        if (res) {
          notification.success();
        }
      });
    }
    /**
     * 获取表单的值
     *
     * @param {*} values
     * @memberof MessageQuery
     */

  }, {
    key: "storeFormValues",
    value: function storeFormValues(values) {
      this.setState({
        formValues: _objectSpread({}, values)
      });
    }
    /**
     * 表单查询
     */

  }, {
    key: "handleMultiSearchFormSearch",
    value: function handleMultiSearchFormSearch() {
      var fieldsValue = getRefFieldsValue(this.multiSearchFormRef);
      var values = fieldsValue;
      values = {
        startDate: fieldsValue.startDate ? fieldsValue.startDate.format(DEFAULT_DATETIME_FORMAT) : undefined,
        endDate: fieldsValue.endDate ? fieldsValue.endDate.format(DEFAULT_DATETIME_FORMAT) : undefined
      };
      this.handleQueryMessage(_objectSpread(_objectSpread({}, fieldsValue), values));
    }
  }, {
    key: "handleSelectChange",
    value: function handleSelectChange(rows) {
      this.setState({
        selectedRows: rows
      });
    }
  }, {
    key: "handleDelete",
    value: function handleDelete() {
      var _this2 = this;

      var dispatch = this.props.dispatch;
      var _this$state$selectedR = this.state.selectedRows,
          selectedRows = _this$state$selectedR === void 0 ? [] : _this$state$selectedR;
      dispatch({
        type: 'messageQuery/deleteMessage',
        payload: selectedRows
      }).then(function (res) {
        if (res) {
          notification.success();

          _this2.handleMultiSearchFormSearch();
        }
      });
    }
  }, {
    key: "handlePageChange",
    value: function handlePageChange() {
      var pagination = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
      var fieldsValue = arguments.length > 1 ? arguments[1] : undefined;
      this.handleQueryMessage(_objectSpread({
        page: pagination
      }, fieldsValue));
    }
  }, {
    key: "render",
    value: function render() {
      var _this$props = this.props,
          queryMessageLoading = _this$props.queryMessageLoading,
          queryRecipientLoading = _this$props.queryRecipientLoading,
          queryContentLoading = _this$props.queryContentLoading,
          queryErrorLoading = _this$props.queryErrorLoading,
          resendLoading = _this$props.resendLoading,
          _this$props$deleteLoa = _this$props.deleteLoading,
          deleteLoading = _this$props$deleteLoa === void 0 ? false : _this$props$deleteLoa,
          path = _this$props.match.path,
          _this$props$messageQu = _this$props.messageQuery,
          _this$props$messageQu2 = _this$props$messageQu.messageData,
          messageData = _this$props$messageQu2 === void 0 ? {} : _this$props$messageQu2,
          _this$props$messageQu3 = _this$props$messageQu.content,
          content = _this$props$messageQu3 === void 0 ? {} : _this$props$messageQu3,
          _this$props$messageQu4 = _this$props$messageQu.recipientData,
          recipientData = _this$props$messageQu4 === void 0 ? {} : _this$props$messageQu4,
          _this$props$messageQu5 = _this$props$messageQu.error,
          error = _this$props$messageQu5 === void 0 ? {} : _this$props$messageQu5,
          _this$props$messageQu6 = _this$props$messageQu.statusList,
          statusList = _this$props$messageQu6 === void 0 ? [] : _this$props$messageQu6,
          _this$props$messageQu7 = _this$props$messageQu.messageTypeList,
          messageTypeList = _this$props$messageQu7 === void 0 ? [] : _this$props$messageQu7,
          tenantRoleLevel = _this$props.tenantRoleLevel;
      var _this$state = this.state,
          contentVisible = _this$state.contentVisible,
          recipientVisible = _this$state.recipientVisible,
          isContent = _this$state.isContent,
          _this$state$selectedR2 = _this$state.selectedRows,
          selectedRows = _this$state$selectedR2 === void 0 ? [] : _this$state$selectedR2,
          _this$state$recordDat = _this$state.recordData,
          recordData = _this$state$recordDat === void 0 ? {} : _this$state$recordDat,
          _this$state$formValue = _this$state.formValues,
          formValues = _this$state$formValue === void 0 ? {} : _this$state$formValue;
      var formProps = {
        messageTypeList: messageTypeList,
        statusList: statusList,
        tenantRoleLevel: tenantRoleLevel,
        onQueryMessage: this.handlePageChange,
        onStoreFormValues: this.storeFormValues,
        wrappedComponentRef: this.multiSearchFormRef,
        onSearch: this.handleMultiSearchFormSearch
      };
      var tableProps = {
        messageData: messageData,
        formValues: formValues,
        tenantRoleLevel: tenantRoleLevel,
        loading: queryMessageLoading,
        onSelectChange: this.handleSelectChange,
        onOpenRecipientModal: this.handleOpenRecipientModal,
        onOpenContentModal: this.handleOpenContentModal,
        onOpenErrorModal: this.handleOpenErrorModal,
        onQueryMessage: this.handleQueryMessage,
        onResendMessage: this.handleResendMessage,
        path: path,
        resendLoading: resendLoading
      };
      var contentViewProps = {
        isContent: isContent,
        contentVisible: contentVisible,
        content: content,
        error: error,
        loading: queryContentLoading || queryErrorLoading || false,
        onOk: this.handleOk,
        path: path
      };
      var recipientViewProps = {
        recipientVisible: recipientVisible,
        recipientData: recipientData,
        recordData: recordData,
        loading: queryRecipientLoading,
        onOk: this.handleRecipientOk,
        onOpenRecipientModal: this.handleOpenRecipientModal,
        path: path
      };
      return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(Header, {
        title: intl.get('cmsg.messageQuery.view.message.title').d('消息查询')
      }, /*#__PURE__*/React.createElement(ButtonPermission, {
        permissionList: [{
          code: "".concat(path, ".button.delete"),
          type: 'button',
          meaning: '消息查询-删除'
        }],
        type: "primary",
        icon: "delete",
        onClick: this.handleDelete,
        disabled: selectedRows.length === 0,
        loading: deleteLoading
      }, intl.get('cpaas.common.status.delete').d('删除'))), /*#__PURE__*/React.createElement(Content, null, /*#__PURE__*/React.createElement(QueryForm, formProps), /*#__PURE__*/React.createElement(ListTable, tableProps)), /*#__PURE__*/React.createElement(ContentView, contentViewProps), /*#__PURE__*/React.createElement(RecipientView, recipientViewProps));
    }
  }]);

  return MessageQuery;
}(PureComponent), (_applyDecoratedDescriptor(_class2.prototype, "handleQueryMessage", [_dec3], Object.getOwnPropertyDescriptor(_class2.prototype, "handleQueryMessage"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "handleOpenContentModal", [_dec4], Object.getOwnPropertyDescriptor(_class2.prototype, "handleOpenContentModal"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "handleOk", [_dec5], Object.getOwnPropertyDescriptor(_class2.prototype, "handleOk"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "handleOpenRecipientModal", [_dec6], Object.getOwnPropertyDescriptor(_class2.prototype, "handleOpenRecipientModal"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "handleRecipientOk", [_dec7], Object.getOwnPropertyDescriptor(_class2.prototype, "handleRecipientOk"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "handleOpenErrorModal", [_dec8], Object.getOwnPropertyDescriptor(_class2.prototype, "handleOpenErrorModal"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "handleResendMessage", [_dec9], Object.getOwnPropertyDescriptor(_class2.prototype, "handleResendMessage"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "storeFormValues", [_dec10], Object.getOwnPropertyDescriptor(_class2.prototype, "storeFormValues"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "handleMultiSearchFormSearch", [_dec11], Object.getOwnPropertyDescriptor(_class2.prototype, "handleMultiSearchFormSearch"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "handleSelectChange", [_dec12], Object.getOwnPropertyDescriptor(_class2.prototype, "handleSelectChange"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "handleDelete", [_dec13], Object.getOwnPropertyDescriptor(_class2.prototype, "handleDelete"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "handlePageChange", [_dec14], Object.getOwnPropertyDescriptor(_class2.prototype, "handlePageChange"), _class2.prototype)), _class2)) || _class) || _class);
export { MessageQuery as default };